package com.jrzh.mvc.controller.sys.ajax;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.jrzh.bean.KindEditorBean;
import com.jrzh.bean.SplitFileBean;
import com.jrzh.bean.UploadBean;
import com.jrzh.bean.layer.LayerImageBean;
import com.jrzh.bean.layer.LayerPhotoBean;
import com.jrzh.common.cache.Cache;
import com.jrzh.common.cache.CacheManager;
import com.jrzh.common.tools.CollectionTools;
import com.jrzh.common.utils.DateUtil;
import com.jrzh.common.utils.ImgUtil;
import com.jrzh.config.ProjectConfigration;
import com.jrzh.framework.base.controller.BaseAjaxController;
import com.jrzh.framework.bean.ResultBean;
import com.jrzh.mvc.constants.CacheConstants;
import com.jrzh.mvc.model.sys.FileModel;
import com.jrzh.mvc.model.sys.UserModel;
import com.jrzh.mvc.search.sys.FileSearch;
import com.jrzh.mvc.service.sys.manage.SysServiceManage;
import com.jrzh.mvc.view.sys.FileView;

@SuppressWarnings("all")
@RestController(FileController.LOCATION + "FileController")
@RequestMapping(FileController.LOCATION)
public class FileController extends BaseAjaxController{
	public static final String LOCATION = "/sys/ajax/file/";
	
	@Autowired
	SysServiceManage sysServiceManage;
	
	@Autowired
	ProjectConfigration projectConfigration;
	
	@PostMapping(value = "upload")
    public UploadBean upload(HttpServletRequest request) throws IOException{
		UploadBean result = new UploadBean();
		String model = request.getParameter("model");
		String uploadName = request.getParameter("fileName");
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		MultipartFile multipartFile =null;
		String fileName = null;
		String dateDir = DateUtil.format(new Date(), "yyyy-MM-dd");
		String ctxPath = projectConfigration.getFileRootUrl()+"file/" + model+ "/" + dateDir +"/";
		String mapPath = projectConfigration.mappingUrl+"file/" + model+ "/" + dateDir+"/";
		File file = new File(ctxPath);
		if(!file.exists()) file.mkdirs();
		for(Map.Entry<String,MultipartFile > set:fileMap.entrySet()){
			multipartFile = set.getValue();//文件名
			fileName = multipartFile.getOriginalFilename();
            String uuid = UUID.randomUUID().toString().replaceAll("\\-", "");// 返回一个随机UUID。
            String suffix = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf("."), fileName.length()) : null;
            String newFileName =  fileName+ "-" + uuid + (suffix!=null?suffix:"");// 构成新文件名。
            File uploadFile = new File(file.getPath() + "/"+newFileName);
            try {  
                FileCopyUtils.copy(multipartFile.getBytes(), uploadFile); 
                result.setAbsoluteFilePath(ctxPath+newFileName);
                result.setRelativeFilePath(mapPath + newFileName);
                if(StringUtils.isNotBlank(uploadName)){
                	fileName = uploadName;
                }else{
                	fileName = fileName.substring(0,fileName.indexOf("."));
                }
                result.setFileName(fileName);
                result.setFileType(suffix);
                result.setStatus(UploadBean.Status_Success);
            }catch (Exception e) {
            	e.printStackTrace();
			}
		}
        return result;
    }
	
	//根据url获取复数文件
	@PostMapping(value = "getFilesByUrls")
	public List<FileView> getFilesByUrls(HttpServletRequest request){
		List<FileView> fileList = new ArrayList<FileView>();
		try {
			String url = request.getParameter("urls");
			String regx = request.getParameter("regx");
			if(StringUtils.isNotBlank(url)){
				String[] urls = StringUtils.split(url, regx);
				for (String u : urls) {
					FileView view = sysServiceManage.fileService.findViewByField("url", u);
					if(null != view){
						fileList.add(view);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fileList;
	}
	
	//根据key获取复数文件
	@PostMapping(value = "getFilesByKey")
	public List<FileView> getFilesByKey(HttpServletRequest request,HttpSession session){
		List<FileView> fileList = new ArrayList<FileView>();
		try {
			String fileKey = request.getParameter("fileKey");
			Map<String, String[]> fileMap = new HashMap<String, String[]>();
			Object object=session.getAttribute(fileKey);
			if(null!=object){
				fileMap=(HashMap<String, String[]>)object;
				String[] fileNames=fileMap.get(CacheConstants.CACHE_FILE.FILE_NAMES);
				String[] fileTypes=fileMap.get(CacheConstants.CACHE_FILE.FILE_TYPES);
				String[] fileUrls=fileMap.get(CacheConstants.CACHE_FILE.FILE_URLS);
				if(null!=fileNames&&fileNames.length>0){
				  for(int i=0;i<fileNames.length;i++){
					FileView fileView=new FileView();
					fileView.setName(fileNames[i]);
					fileView.setType(fileTypes[i]);
					fileView.setUrl(fileUrls[i]);
					fileList.add(fileView);
				  }
			    }
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fileList;
	}
	
	@PostMapping(value = "getFileNumByKey")
	public Integer getFileNumByKey(HttpServletRequest request,HttpSession session){
		Integer num=0;
		try {
			 
			String fileKey = request.getParameter("fileKey");
			Map<String, String[]> fileMap = new HashMap<String, String[]>();
			Object object=session.getAttribute(fileKey);
			if(null!=object){
				fileMap=(HashMap<String, String[]>)object;
				String[] fileNames=fileMap.get(CacheConstants.CACHE_FILE.FILE_NAMES);
				String[] fileTypes=fileMap.get(CacheConstants.CACHE_FILE.FILE_TYPES);
				String[] fileUrls=fileMap.get(CacheConstants.CACHE_FILE.FILE_URLS);
				if(null!=fileNames&&fileNames.length>0){
					num=fileNames.length;
			    }
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return num;
	}
	
	
	@PostMapping(value = "deleteFileByUrl")
	public ResultBean deleteFileByUrl(){
		ResultBean result = new ResultBean();
		try {
			String url = request.getParameter("url");
			FileModel file = sysServiceManage.fileService.findByField("url", url);
			if(null!=file){
			   sysServiceManage.fileService.delete(file.getId(), getSessionUser());
		    }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@PostMapping(value = "getFileByUrl")
	public FileView getFileByUrl(HttpServletRequest request){
		FileView file = new FileView();
		try {
			String url = request.getParameter("url");
			file = sysServiceManage.fileService.findViewByField("url", url);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return file;
	}

	@PostMapping(value = "uploadByDropzone")
    public List<UploadBean> uploadBydropzone(HttpServletRequest request) throws IOException{
		List<UploadBean> uploadBeanList = new ArrayList<UploadBean>();
		String model = request.getParameter("model");
		String uploadName ="";// request.getParameter("fileName");
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		List<MultipartFile> fileList = multipartRequest.getFiles("files[]");
		String fileName = null;
		String dateDir = DateUtil.format(new Date(), "yyyy-MM-dd");
		String ctxPath = projectConfigration.getFileRootUrl()+"file/" + model+ "/" + dateDir +"/";
		String mapPath = projectConfigration.mappingUrl+"file/" + model+ "/" + dateDir+"/";
		File file = new File(ctxPath);
		if(!file.exists()) file.mkdirs();
		for(MultipartFile multipartFile :fileList){
			UploadBean result = new UploadBean();
			fileName = multipartFile.getOriginalFilename();
            String uuid = UUID.randomUUID().toString().replaceAll("\\-", "");// 返回一个随机UUID。
            String suffix = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf("."), fileName.length()) : null;
            String newFileName =  fileName+ "-" + uuid + (suffix!=null?suffix:"");// 构成新文件名。
            File uploadFile = new File(file.getPath() + "/"+newFileName);
            try {  
                FileCopyUtils.copy(multipartFile.getBytes(), uploadFile); 
                result.setAbsoluteFilePath(ctxPath+newFileName);
                result.setRelativeFilePath(mapPath + newFileName);
                if(StringUtils.isNotBlank(uploadName)){
                	fileName = uploadName;
                }else{
                	fileName = fileName.substring(0,fileName.indexOf("."));
                }
                result.setFileName(fileName);
                result.setFileType(suffix);
                result.setStatus(UploadBean.Status_Success);
            }catch (Exception e) {
            	e.printStackTrace();
			}
            uploadBeanList.add(result);
		}
        return uploadBeanList;
    }
	
	
	@PostMapping(value = "getFileByFormId")
	public List<FileModel> getFileByGroupId(HttpServletRequest request){
		List<FileModel> result = new ArrayList<FileModel>();
		try {
			String formId = request.getParameter("formId");
			if(StringUtils.isNotBlank(formId)){
				FileSearch search = new FileSearch();
				search.setEqualFormId(formId);
				result = sysServiceManage.fileService.list(search);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@PostMapping(value = "deleteFile")
	public ResultBean deleteFile(){
		ResultBean result = new ResultBean();
		try {
			String fileId = request.getParameter("fileId");
			log.info(fileId);
			FileModel file = sysServiceManage.fileService.findById(fileId);
			if(null != file){
				sysServiceManage.fileService.deleteFile(file, getSessionUser());
			}
			result.setStatus("success");
			result.setMsg("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@PostMapping(value = "deleteFileModel")
	public ResultBean deleteFileModel(){
		ResultBean result = new ResultBean();
		try {
			String fileId = request.getParameter("fileId");
			FileModel file = sysServiceManage.fileService.findById(fileId);
			if(null != file){
				sysServiceManage.fileService.deleteModel(file, getSessionUser());
			}
			result.setStatus("success");
			result.setMsg("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@PostMapping(value = "saveFile")
	public ResultBean saveFile(){
		ResultBean result = new ResultBean();
		try {
			String dataId = request.getParameter("dataId");
			String fileKey = request.getParameter("fileKey");
			CacheManager.clearOnly(dataId+fileKey);
			String[] fileNames = request.getParameter(CacheConstants.CACHE_FILE.FILE_NAMES).split(",");
			String[] fileUrls = request.getParameter(CacheConstants.CACHE_FILE.FILE_URLS).split(",");
			String[] fileTypes = request.getParameter(CacheConstants.CACHE_FILE.FILE_TYPES).split(",");
			Map<String, String[]> fileMap = new HashMap<String, String[]>();
			fileMap.put(CacheConstants.CACHE_FILE.FILE_NAMES, fileNames);
			fileMap.put(CacheConstants.CACHE_FILE.FILE_URLS, fileUrls);
			fileMap.put(CacheConstants.CACHE_FILE.FILE_TYPES, fileTypes);
			Cache fileCache = new Cache();
			fileCache.setKey(fileKey);
			fileCache.setValue(fileMap);
			CacheManager.putCache(dataId+fileKey, fileCache);
			result.setStatus(ResultBean.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@PostMapping(value = "getCacheFile")
	public ResultBean getCacheFile(){
		ResultBean result = new ResultBean();
		try {
			String dataId = request.getParameter("dataId");
			String fileKey = request.getParameter("fileKey");
			Cache fileCache = CacheManager.getCacheInfo(dataId+fileKey);
			if(null != fileCache){
				Map<String, String[]> fileMap = (HashMap<String, String[]>)fileCache.getValue();
				result.setObjs(new Object[]{fileMap});
			}
			result.setStatus(ResultBean.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@PostMapping(value = "getFileById")
	public FileView getFileById(HttpServletRequest request){
		FileView result = new FileView();
		try {
			String id = request.getParameter("id");
			if(StringUtils.isNotBlank(id)){
				result = sysServiceManage.fileService.findViewById(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 分段上传文件
	 */
	@PostMapping(value = "uploadSplit")
    public ResultBean uploadSplit(SplitFileBean filebean) throws IOException{
		ResultBean result = new ResultBean();
		String url = "";
		String fileKey = filebean.getFileKey();
		String fileType = filebean.getFileType();
		String fileName = filebean.getFileName();
		for(String imageUrl : filebean.getImageUrl()){
			String[] imageArr=imageUrl.split(",");
			url += imageArr[1]; 
		}
		
		//base64解码
		String base64Code = request.getParameter("base64Code");
		Base64 base64 = new Base64();
		byte[] decodedBytes = base64.decodeBase64(new String(url).getBytes());
/*		BASE64Decoder decoder = new BASE64Decoder();
		byte[] decodedBytes = decoder.decodeBuffer(url);*/
		
		//保存图片
		String dateDir = DateUtil.format(new Date(), "yyyy-MM-dd");
		String ctxPath = projectConfigration.getFileRootUrl()+"file/" + fileKey+ "/" + dateDir +"/";
		String mapPath = projectConfigration.mappingUrl+"file/" + fileKey+ "/" + dateDir+"/";
		File file = new File(ctxPath);
		if(!file.exists()) file.mkdirs();
		String uuid = UUID.randomUUID().toString().replaceAll("\\-", "");// 返回一个随机UUID。
		log.info("文件类型>>>"+fileType);
		if(StringUtils.equals(fileType, "application/vnd.ms-excel")){
			fileType = "application/xls";
		}
		if(StringUtils.equals(fileType, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")){
			fileType = "application/xlsx";
		}
        String suffix = "." + fileType.split("/")[1];
        String newFileName = fileName + "-" + uuid + (suffix!=null?suffix:"");// 构成新文件名。
        File uploadFile = new File(file.getPath() + "/"+newFileName);
        try {  
            FileCopyUtils.copy(decodedBytes, uploadFile);
            FileModel fileModel = new FileModel();
            fileModel.setFormId(filebean.getDataId());
            fileModel.setMemberId(filebean.getMemberId());
            fileModel.setKey(fileKey);
            fileModel.setUrl(mapPath + newFileName);
            fileModel.setName(fileName);
            fileModel.setType(suffix);
            fileModel.setModel("fileRule");
            sysServiceManage.fileService.add(fileModel, getSessionUser());
            result.setMsg("上传成功");
            result.setStatus(ResultBean.SUCCESS);
        }catch (Exception e) {
        	e.printStackTrace();
        	result.setMsg(e.getMessage());
		}
        return result;
    }
	
	
	
	@RequestMapping(method=RequestMethod.POST, value="uploadKind")
	@PostMapping(value = "uploadKind")
    public KindEditorBean uploadKind(HttpServletRequest request) throws IOException{
		KindEditorBean result = new KindEditorBean();
		String model = "uploadKind";
		String uploadName = request.getParameter("fileName");
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		MultipartFile multipartFile =null;
		String fileName = "";
		String dateDir = DateUtil.format(new Date(), "yyyy-MM-dd");
		String ctxPath = projectConfigration.getFileRootUrl() + "file/" + model+ "/" + dateDir +"/";
		String mapPath = projectConfigration.mappingUrl + "file/" + model+ "/" + dateDir+"/";
		File file = new File(ctxPath);
		if(!file.exists()) file.mkdirs();
		for(Map.Entry<String,MultipartFile > set:fileMap.entrySet()){
			multipartFile = set.getValue();//文件名
			fileName = multipartFile.getOriginalFilename();
            String uuid = UUID.randomUUID().toString().replaceAll("\\-", "");// 返回一个随机UUID。
            String suffix = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf("."), fileName.length()) : null;
            String newFileName =  fileName+ "-" + uuid + (suffix!=null?suffix:"");// 构成新文件名。
            File uploadFile = new File(file.getPath() + "/"+newFileName);
            try {  
                FileCopyUtils.copy(multipartFile.getBytes(), uploadFile);
                result.setUrl(mapPath + newFileName);
                result.setTitle(uploadFile.getName());
                if(StringUtils.isNotBlank(uploadName)){
                	fileName = uploadName;
                }else{
                	fileName = fileName.substring(0,fileName.indexOf("."));
                }
                result.setError(KindEditorBean.SUCCESS);
            }catch (Exception e) {
            	e.printStackTrace();
            	String errorMsg = e.getMessage();
            	if(StringUtils.isBlank(errorMsg)){
            		errorMsg = "系统异常";
            	}
            	result.setMessage(errorMsg);
			}
		}
        return result;
    }
	
	@PostMapping(value = "photos/{formId}")
	public LayerPhotoBean photos(@PathVariable("formId") Long formId){
		LayerPhotoBean photo = new LayerPhotoBean();
		try {
			photo.setId(String.valueOf(formId));
			photo.setStart(0);
			photo.setTitle("查看图片");
			List<LayerImageBean> imageList = new ArrayList<LayerImageBean>();
			//是否需要改进
			List<FileModel> fileList = sysServiceManage.fileService.selectByField("formId", formId);
			if(CollectionTools.isNotBlank(fileList)){
				for(FileModel file : fileList){
					LayerImageBean image = new LayerImageBean();
					image.setAlt(file.getName());
					image.setPid(file.getId());
					image.setSrc(file.getUrl());
					image.setThumb(file.getUrl());
					imageList.add(image);
				}
			}
			photo.setData(imageList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return photo;
	}
	
	//上传头像
	@PostMapping(value = "uploadCropper")
    public UploadBean uploadCropper() throws IOException{
		UploadBean result = new UploadBean();
		String model = request.getParameter("model");
		String fileName = request.getParameter("fileName");
		String imageUrl = request.getParameter("imageUrl");
		String header ="data:image"; 
		String[] imageArr=imageUrl.split(",");
		if(imageArr[0].contains(header)){
			// 去掉头部  
			imageUrl=imageArr[1]; 
			
			//base64解码
/*			BASE64Decoder decoder = new BASE64Decoder();
			byte[] decodedBytes = decoder.decodeBuffer(imageUrl);*/
			
			String base64Code = request.getParameter("base64Code");
			Base64 base64 = new Base64();
			byte[] decodedBytes = base64.decodeBase64(new String(imageUrl).getBytes());
			
			//保存图片
			String dateDir = DateUtil.format(new Date(), "yyyy-MM-dd");
			String ctxPath = projectConfigration.getFileRootUrl()+"file/" + model+ "/" + dateDir +"/";
			String mapPath = projectConfigration.mappingUrl+"file/" + model+ "/" + dateDir+"/";
			File file = new File(ctxPath);
			if(!file.exists()) file.mkdirs();
			String uuid = UUID.randomUUID().toString().replaceAll("\\-", "");// 返回一个随机UUID。
            String suffix = ".png";
            String newFileName =  fileName+ "-" + uuid + (suffix!=null?suffix:"");// 构成新文件名。
            File uploadFile = new File(file.getPath() + "/"+newFileName);
            try {  
                FileCopyUtils.copy(decodedBytes, uploadFile);
                result.setAbsoluteFilePath(ctxPath+newFileName);
                result.setRelativeFilePath(mapPath + newFileName);
                result.setFileName(newFileName);
                result.setFileType(suffix);
                UserModel user = sysServiceManage.userService.findByField("code", fileName);
                user.setUrl(mapPath+newFileName);
                user.setType(suffix);
                sysServiceManage.userService.edit(user, getSessionUser());
                getSessionUser().setPhoto(user.getUrl());
                result.setStatus(UploadBean.Status_Success);
            }catch (Exception e) {
            	e.printStackTrace();
			}
		}
        return result;
    }
	
	//跟进fileKey上传图片
	@PostMapping(value = "uploadKeyFile")
    public ResultBean uploadKeyFile() throws IOException{
		ResultBean result = new ResultBean();
		String fileKey = request.getParameter("fileKey");
		String fileType = request.getParameter("fileType");
		String fileName = request.getParameter("fileName");
		String imageUrl = request.getParameter("imageUrl");
		String dataId = request.getParameter("dataId");
		String index = request.getParameter("index");
		String memberId = request.getParameter("memberId");
		String[] imageArr=imageUrl.split(",");
		imageUrl=imageArr[1]; 
			//base64解码
/*			BASE64Decoder decoder = new BASE64Decoder();
			byte[] decodedBytes = decoder.decodeBuffer(imageUrl);*/
			String base64Code = request.getParameter("base64Code");
			Base64 base64 = new Base64();
			byte[] decodedBytes = base64.decodeBase64(new String(imageUrl).getBytes());
			
			
			//保存图片
			String dateDir = DateUtil.format(new Date(), "yyyy-MM-dd");
			String ctxPath = projectConfigration.getFileRootUrl()+"file/" + fileKey+ "/" + dateDir +"/";
			String mapPath = projectConfigration.mappingUrl+"file/" + fileKey+ "/" + dateDir+"/";
			File file = new File(ctxPath);
			if(!file.exists()) file.mkdirs();
			String uuid = UUID.randomUUID().toString().replaceAll("\\-", "");// 返回一个随机UUID。
            String suffix = "." + fileType.split("/")[1];
            String newFileName = fileName + "-" + uuid + (suffix!=null?suffix:"");// 构成新文件名。
            File uploadFile = new File(file.getPath() + "/"+newFileName);
            try {  
                FileCopyUtils.copy(decodedBytes, uploadFile);
                FileModel fileModel = new FileModel();
                fileModel.setFormId(dataId);
                fileModel.setMemberId(memberId);
                fileModel.setKey(fileKey);
                fileModel.setUrl(mapPath + newFileName);
                fileModel.setName(fileName);
                fileModel.setType(suffix);
                fileModel.setModel("fileRule");
                sysServiceManage.fileService.add(fileModel, getSessionUser());
                result.setMsg("上传成功");
                if(StringUtils.isNotBlank(index)){
                	result.setObjs(new Object[]{Integer.valueOf(index)+1});
                }
                result.setStatus(ResultBean.SUCCESS);
            }catch (Exception e) {
            	e.printStackTrace();
            	result.setMsg(e.getMessage());
			}
        return result;
    }
	
		//根据文件类型获取文件
		@PostMapping(value = "loadFile")
		public ResultBean loadFile(){
			ResultBean result = new ResultBean();
			try {
				String fileKey = request.getParameter("fileKey");
				String modelId = request.getParameter("modelId");
				FileSearch search = new FileSearch();
				search.setEqKey(fileKey);
				search.setEqualFormId(modelId);
				List<FileView> fileList = sysServiceManage.fileService.viewList(search);
				result.setStatus(ResultBean.SUCCESS);
				result.setObjs(new Object[]{fileList});
			} catch (Exception e) {
				e.printStackTrace();
				result.setMsg(e.getMessage());
			}
			return result;
	}
		
		//根据会员Id获取文件
		@PostMapping(value = "loadMemberFile")
		public ResultBean loadMemberFile(){
			ResultBean result = new ResultBean();
			try {
				String fileKey = request.getParameter("fileKey");
				String memberId = request.getParameter("memberId");
				FileSearch search = new FileSearch();
				search.setEqKey(fileKey);
				search.setEqualMemberId(memberId);
				List<FileView> fileList = sysServiceManage.fileService.viewList(search);
				result.setStatus(ResultBean.SUCCESS);
				result.setObjs(new Object[]{fileList});
			} catch (Exception e) {
				e.printStackTrace();
				result.setMsg(e.getMessage());
			}
			return result;
	}
		
		
	/**
	 * @Description: 壓縮上傳
	 * @param @param request
	 * @param @return
	 * @param @throws IOException   
	 * @return UploadBean  
	 * @throws
	 * @author caijiahong
	 */
	@PostMapping(value = "compressionUpload")
    public UploadBean compressionUpload(HttpServletRequest request) throws IOException{
		UploadBean result = new UploadBean();
		String model = request.getParameter("model");
		String uploadName = request.getParameter("fileName");
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		MultipartFile multipartFile =null;
		String fileName = null;
		String dateDir = DateUtil.format(new Date(), "yyyy-MM-dd");
		String ctxPath = projectConfigration.getFileRootUrl()+"file/" + model+ "/" + dateDir +"/";
		String mapPath = projectConfigration.mappingUrl+"file/" + model+ "/" + dateDir+"/";
		File file = new File(ctxPath);
		if(!file.exists()) file.mkdirs();
		String fileStr = "_compress";
		for(Map.Entry<String,MultipartFile > set:fileMap.entrySet()){
			multipartFile = set.getValue();//文件名
			fileName = multipartFile.getOriginalFilename();
            String uuid = UUID.randomUUID().toString().replaceAll("\\-", "");// 返回一个随机UUID。
            String suffix = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf("."), fileName.length()) : null;
            String newFileName =  fileName+ "-" + uuid + (suffix!=null?suffix:"");// 构成新文件名。
            String compressFileName = fileName+ "-" + uuid + fileStr + (suffix!=null?suffix:"");
            File uploadFile = new File(file.getPath() + "/"+newFileName);
            try {  
                FileCopyUtils.copy(multipartFile.getBytes(), uploadFile); 
                ImgUtil.zipImageFile(ctxPath+newFileName, 0.1f, fileStr);
            	/*//壓縮上傳
            	ImageHelper.compress(multipartFile.getInputStream(), uploadFile, 150);*/
                
            	result.setAbsoluteFilePath(ctxPath+compressFileName);
                result.setRelativeFilePath(mapPath + compressFileName);
                if(StringUtils.isNotBlank(uploadName)){
                	fileName = uploadName;
                }else{
                	fileName = fileName.substring(0,fileName.indexOf("."));
                }
                result.setFileName(fileName);
                result.setFileType(suffix);
                result.setStatus(UploadBean.Status_Success);
            }catch (Exception e) {
            	e.printStackTrace();
			}
		}
        return result;
    }
		
	@Override
	protected void setData() {
	}
}
